﻿using FI.Core.IoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FI.Core
{
    [MapTo(typeof(IMessageResolverFactory), -1, Lifetime = Lifetime.Singleton)]
    internal class MessageResolverFactory : IMessageResolverFactory
    {
        public IMessageResolver GetMessageResolver(string messageId, string messageCategory, string resourceName, string resourceType, string fallbackValue)
        {
          
            return new MessageResolver(fallbackValue);
        }

        public IMessageResolver GetMessageResolver(string messageId, string messageCategory, string resourceName, Type resourceType, string fallbackValue)
        {
            return new MessageResolver(fallbackValue);
        }
    }

    internal class MessageResolver : IMessageResolver
    {
        private string message;
        public MessageResolver(string message)
        {
            this.message = message;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])")]
        public string GetMessage(params object[] args)
        {
            if (string.IsNullOrEmpty(message))
            {
                return string.Empty;
            }

            if (message.Contains("{0}") && (args??new object[0]).Any())
            {
                return string.Format(message, args);
            }
            return message;
        }
    }
}
